#include "draw_svg.h"
#include <mupdf/fitz.h>

void draw_svg::draw_page(float resolution,float rotation,fz_rect& mediabox,fz_device* dev,
        int output_format,fz_output * out,fz_context* ctx,fz_page *page,int lowmemory,fz_display_list *list
        ,const char * ocr_language,const char * ocr_datadir,fz_cookie *cookie,int pagenum,
        fz_buffer* out_buffer,std::vector<std::string>& rets
        ){

		float zoom;
		fz_matrix ctm;
		fz_rect tbounds;
		char buf[512];
		fz_output *outs = NULL;

		fz_var(outs);

		zoom = resolution / 72;
		ctm = fz_pre_rotate(fz_scale(zoom, zoom), rotation);
		tbounds = fz_transform_rect(mediabox, ctm);

		fz_try(ctx)
		{
            out_buffer = fz_new_buffer(ctx, 1024);
			outs = fz_new_output_with_buffer(ctx,out_buffer);

			dev = fz_new_svg_device(ctx, outs, tbounds.x1-tbounds.x0, tbounds.y1-tbounds.y0, FZ_SVG_TEXT_AS_PATH, 1);
			if (lowmemory)
				fz_enable_device_hints(ctx, dev, FZ_NO_CACHE);
			if (list)
				fz_run_display_list(ctx, list, dev, ctm, tbounds, cookie);
			else
				fz_run_page(ctx, page, dev, ctm, cookie);
			fz_close_device(ctx, dev);
			fz_close_output(ctx, outs);
            rets.push_back(fz_string_from_buffer(ctx, out_buffer));
		}
		fz_always(ctx)
		{
			fz_drop_device(ctx, dev);
			fz_drop_output(ctx, outs);
		}
		fz_catch(ctx)
		{
			fz_rethrow(ctx);
		}
}
